#include<bits/stdc++.h>

using namespace std;

int Majority(int A[], int n) {
    int c = A[0], count = 1;
    for (int i = 1; i < n; i++) {
        if (A[i] == c)
            count++;
        else {
            count--;
            if (count == 0) {
                c = A[i];
                count = 1;
            }
        }
    }
    count = 0;
    for (int i = 0; i < n; i++)
        if (A[i] == c)
            count++;
    if (count > n / 2)
        return c;
    return -1;
}

int main() {
    //  测试
    int n = 8;
    int num1[] = {0, 5, 5, 3, 5, 7, 5, 5};
    int num2[] = {0, 5, 5, 3, 5, 1, 5, 7};
    cout << Majority(num1, n) << endl;
    cout << Majority(num2, n) << endl;
    return 0;
}